Libraries

<-- Prev --------- Contents --------- Next -->

Remember earlier, we mentioned libraries? A library is a collection of code (which you may have written, or may have been written by someone else), which is compiled and saved outside of your program. A library is meant to be included in other programs so that they may use the code that they contain. C# has tons of built-in libraries, some of which we have been including via the using keyword at the beginning of our programs.

Libraries in programming are extremely useful, because they allow us to re-use code from other programs that we think might be generally useful. For example, suppose we put together a bunch of code that could do various complicated math for us. We might have some code to compute a distance function, some code to compute the volume of various shapes, etc. You can imagine a lot of our future programs might need that code. Rather than copying and pasting all of our old math code into our new programs, we could instead compile all of the math code into a library. Then, when we write a new program, we can simply include the library, add the appropriate using line to our code, and then use the code as normal!

Libraries also encourage modularity in programs. Modularity allows the program to be made and organized in pieces, so that each component of our program is in its own section. This makes the code easy to read, and convenient to work with.

In our case we will be bringing in two libraries that someone else has made for us.

The Vex Library


The Vex Library (VexLib) was written by Ryan Connaughton and handles communication between the Vex Kit and the Computer. It gives us some simple code to work with that will send messages over the Bluetooth connection between the robot and the server. The messages that we send are converted by the VexLib into a format that the robot program can interpret as motor signals, sensor queries, etc.

Download the following file, and save in a location that you can get to later.
Version 1

The WiiMote Library


The WiiMote Library (WiimoteLib) was written by Brian Peek and handles the communication between the WiiMote and the Computer. It is very similar to the VexLib, except that the messages it sends and receives for us are via the Wiimote's Bluetooth communication. We will use it to get the state of the Wiimote buttons, accelerometers, and camera later on.

We are currently using version 1.5.2. Newer versions have not been tested but may still work. See here for more information on the WiimoteLib.

Download the WiimoteLib now, and save it with the VexLib for later.

Inserting Libraries in Visual C#


Now that we know what libraries are and have downloaded the new libraries that we will need, let's take a look at how to add them to a project.

Go ahead and create a new project, called LibraryTest.

All this program is going to do is include the two new libraries, and make sure that they are included properly.

The first thing that we will need to do is tell Visual C# where it can find these new libraries on your computer. Remember the Solution Explorer, which lists all of the files and resources in your project? Well this is where it comes into play. Right now, it should look like this:
solution_explorer.JPG

To insert a library in Visual C#, we need to use the References section.

Right-click the word "References", and click "Add Reference". It may take a moment.
csharp_include1.JPG

After a moment, it should bring up the Add Reference window. Select the browse tab. Use this window to browse to your VexLib.dll and WiimoteLib.dll files. Select both of them, and hit OK, as shown below.
includinglibs.jpg

If done correctly, you should see WiimoteLib and VexLib listed in the Solution Explorer under References (you will have to click the plus sign next to References to expand the category).

Visual C# now knows where to find the libraries! There's one thing left to do to include them in our projects.

Let's focus back on our Program.cs file on the left. Our code should still look like a normal, boring program. Now, we need to add the using lines to tell C# that we will be using code from these libraries. The lines look like this:
using WiimoteLib;
using VexLib;
Go ahead and add those lines to your program, just below the other using statements. It should look like this:
usinglibs.jpg
We've now done everything we need to do to use the Wiimote and VexLib libraries!

Let's test it out. To test it, we'll insert two lines of code that make use of each of the libraries. It won't do much, but if it runs without errors, we'll know that it was able to get at the code saved in those libraries.

Add the following two lines to your Main function:
VexConnection vc;
Wiimote wii;

It should look like this:
testing_the_libs.jpg

Go ahead and compile and run your program. It should open a console and close it again without any errors (there might be warnings about unused variables, but that's fine). Congratulations- you just used two new libraries!

Now, it's time to use them to control the robot!


<-- Prev --------- Contents --------- Next -->